package servlet.homework;

import bean.Product;
import service.impl.ProductServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
@WebServlet("/p.do")
public class ProductList extends HttpServlet {
    ProductServiceImpl productService = new ProductServiceImpl();


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


        String id = req.getParameter("id");
        if (id!=null) {
            productService.deleteById(Integer.parseInt(id));
        }

        ArrayList<Product> products = productService.queryAll();
        req.setAttribute("ProductList",products);
        req.getRequestDispatcher("/JSP/productList.jsp").forward(req,resp);

    }
}
